package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;

public class Search_DB {

	//测试用
//	public static void main(String[] args) {
//		Search_DB eDb=new Search_DB();
//		eDb.search_results("hyks");
//	}

	public List<HashMap<String, String>> search_results(String spot_name) {
		Connection con = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		String driver = "com.mysql.cj.jdbc.Driver";
		String url = "jdbc:mysql://182.92.239.30:3306/ks?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
		String user = "root";
		String password = "root";
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		// 选取数据库方法
		System.out.println("进入数据库这里的数据：" + "spot_name:" + spot_name);
		String sql = sel_db(spot_name);
		Gson gson = new Gson();
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, user, password);

		} catch (Exception e) {
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
		try {
//			preparedStatement=con.prepareStatement("select * from 5mine_quack_results where type like '%"+dataString+"%'");
			preparedStatement = con.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("x", resultSet.getString(3));
				map.put("y", resultSet.getString(4));
				map.put("quackTime", resultSet.getString(6));
				map.put("quackGrade", resultSet.getString(7));
				map.put("nengliang", resultSet.getString(11));
				list.add(map);
			}
		} catch (Exception e) {
			System.out.println("连接失败");
			e.printStackTrace();
		}
		// 打印json——测试
		String json = gson.toJson(list);
		System.out.println(json);
		return list;
	}

	// 台站工况REST

	private String sel_db(String spot_name) {
		// TODO Auto-generated method stub
		String sql = "select * from mine_quake_results_hongyang ORDER BY  quackTime";
		// 进行筛选

		// 1.红阳矿山数据源入口
		if (spot_name.equals("hyks")) {
			sql = "select * from mine_quake_results_hongyang ORDER BY  quackTime";
		}

		// 2.河南平顶山数据源入口
		if (spot_name.equals("pds")) {

		}

		// 3.山西塔山数据源入口
		if (spot_name.equals("sxts")) {

		}

		// 4.。。。等等
		return sql;
	}
}
